package com.cts.learninglibrary.handler;

import com.cts.learninglibrary.exception.AuthException;
import com.cts.learninglibrary.exception.LoginErrorException;
import com.cts.learninglibrary.util.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author arctique
 * @date 2020/8/24 15:52
 */
@RestControllerAdvice
@Component
@Slf4j
public class AllExceptionHandler {

    /**
     * 登录异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(LoginErrorException.class)
    @ResponseBody
    public R loginErrorExceptionHandler(LoginErrorException e) {
        return R.failed(e.getCode(), e.getMessage());
    }

    /**
     * 授权拦截异常处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(AuthException.class)
    @ResponseBody
    public R authExceptionHandler(AuthException e) {
        return R.failed(e.getCode(), e.getMessage());
    }

    /**
     * 登陆出错统一异常处理
     *
     * @return
     */
    @ExceptionHandler(IncorrectCredentialsException.class)
    @ResponseBody
    public R handlerIncorrectCredentialsException() {
        return R.failed("账号/密码错误");
    }
}
